
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE [dbo].[HistorySelectRange]
	@UserId INT,
	@StartDate DATETIME,
	@EndDate DATETIME
AS
BEGIN
	SELECT HistoryRecord.Id, HistoryRecord.UserId, UserInfo.Login, 
		HistoryRecord.Action, HistoryRecord.Description, 
		HistoryRecord.DateCreated
	FROM HistoryRecord
	LEFT JOIN UserInfo
		ON (HistoryRecord.UserId = UserInfo.Id)
	WHERE HistoryRecord.DELETED = 0 
		AND HistoryRecord.DateCreated BETWEEN @StartDate AND @EndDate
		AND ((@UserId > 0 AND HistoryRecord.UserId = @UserId) OR (@UserId = 0))
	ORDER BY HistoryRecord.DateCreated
END
GO
